﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;


public partial class ViewMatchType : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchType matchType;
    List<MatchType> matchTypes;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["AdminUser"] != null)
            //    SessionManager.CurrentUser = (LoggedInUser)Session["AdminUser"];
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindMatchType();
            }
        }
    }

    private void bindMatchType()
    {
        matchType = new MatchType(_dataObject);
        matchTypes = new List<MatchType>();
        matchTypes = matchType.GetAllMatchType();
        if (matchTypes.Count > 0)
        {
            rptMatchType.DataSource = matchTypes;
            rptMatchType.DataBind();
        }
    }

    protected void rptMatchType_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower().Trim() == "editmatchtype")
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }
        if (e.CommandName.ToString().ToLower().Trim().Equals("deletematchtype"))
        {
            System.Web.UI.WebControls.RepeaterItem row = (System.Web.UI.WebControls.RepeaterItem)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnMatchId = (System.Web.UI.WebControls.Label)row.FindControl("hdnMatchId");
            matchType = new MatchType(_dataObject);
            matchType.Id = Convert.ToInt32(hdnMatchId.Text);
            matchType.DeleteMatchType();
            divRed.InnerHtml = "<button data-dismiss='alert' class='close'></button><strong>Success! </strong>&nbsp;Record deleted successfully!";
            divRed.Attributes.Add("class", "alert alert-success");
            bindMatchType();
        }
    }
    protected void rptMatchType_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Web.UI.WebControls.Label hdnMatchId = (System.Web.UI.WebControls.Label)e.Item.FindControl("hdnMatchId");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Item.FindControl("btnEdit");
            btnEdit.PostBackUrl = "AddMatchType.aspx?Id=" + hdnMatchId.Text;
        }
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddMatchType.aspx");
    }
}